#include "Inventory.h"
#include "GFuns.h"

Inventory::Inventory(EJob aJob, bool aMale)
{
	GenerateClothes(aJob, aMale);
	GenerateSelf(aJob);
	GenerateSell(aJob);
}

Inventory::~Inventory()
{
}

Item Inventory::GenerateItem(EItem aType, int aSubType)
{
	Item item(aType, aSubType);
	return item;
}

void Inventory::GenerateClothes(EJob aJob, bool aMale)
{
	if(aJob == JOB_NONE)
	{
		int clothNumber = rand()%8 + 3;
		for(int i=0; i<clothNumber; i++)
			clothes.push_back(GenerateItem(ITEM_CLOTH,(ECloth)(rand()%(CLOTH_END-1)+1)));
	}

	if(aJob == JOB_UNEMPLOYED)
	{
		if(aMale)
		{
			clothes.push_back(GenerateItem(ITEM_CLOTH,CLOTH_SHIRT));
			clothes.push_back(GenerateItem(ITEM_CLOTH,CLOTH_TROUSERS));
		}
		else
		{
			clothes.push_back(GenerateItem(ITEM_CLOTH,CLOTH_DRESS));
		}
		
	}
	if(aJob == JOB_SOLDIER)
	{
		clothes.push_back(GenerateItem(ITEM_CLOTH,CLOTH_ARMOR));
	}
	if(aJob == JOB_WEAPONSMITH)
	{
		clothes.push_back(GenerateItem(ITEM_CLOTH,CLOTH_APRON_SMITH));
	}
	if(aJob == JOB_ARMORSMITH)
	{
		clothes.push_back(GenerateItem(ITEM_CLOTH,CLOTH_APRON_SMITH));
	}
	if(aJob == JOB_BARTENDER)
	{
		clothes.push_back(GenerateItem(ITEM_CLOTH,CLOTH_CHEMISE));
		clothes.push_back(GenerateItem(ITEM_CLOTH,CLOTH_VEST));
		clothes.push_back(GenerateItem(ITEM_CLOTH,CLOTH_PANTS));
		clothes.push_back(GenerateItem(ITEM_CLOTH,CLOTH_BELT));
	}
	if(aJob == JOB_HERBALIST)
	{
		clothes.push_back(GenerateItem(ITEM_CLOTH,CLOTH_APRON_HERB));
	}
	if(aJob == JOB_KING)
	{
		clothes.push_back(GenerateItem(ITEM_CLOTH,CLOTH_KINGS_COAT));
	}
	if(aJob == JOB_PRIEST)
	{
		clothes.push_back(GenerateItem(ITEM_CLOTH,CLOTH_CASSOCK));
	}
	if(aJob == JOB_SELLER)
	{
		clothes.push_back(GenerateItem(ITEM_CLOTH,CLOTH_SHIRT));
		clothes.push_back(GenerateItem(ITEM_CLOTH,CLOTH_VEST));
		clothes.push_back(GenerateItem(ITEM_CLOTH,CLOTH_STRIP));
		clothes.push_back(GenerateItem(ITEM_CLOTH,CLOTH_TROUSERS));

		if(!aMale)
		{
			clothes.push_back(GenerateItem(ITEM_CLOTH,CLOTH_SKIRT));
		}
	}
	if(aJob == JOB_CHEF)
	{
		clothes.push_back(GenerateItem(ITEM_CLOTH,CLOTH_HAT));
		clothes.push_back(GenerateItem(ITEM_CLOTH,CLOTH_CHEMISE));
		clothes.push_back(GenerateItem(ITEM_CLOTH,CLOTH_PANTS));

		if(!aMale)
		{
			clothes.push_back(GenerateItem(ITEM_CLOTH,CLOTH_PETTICOAT));
		}
	}
	if(aJob == JOB_WORKER)
	{
		clothes.push_back(GenerateItem(ITEM_CLOTH,CLOTH_PANTS));
		clothes.push_back(GenerateItem(ITEM_CLOTH,CLOTH_BELT));
	}

	for(unsigned int i=0; i<clothes.size(); i++)
		clothes[i].SetEquipped(true);
}

void Inventory::GenerateSelf(EJob aJob)
{
	int minItems = 0;
	int maxItems = 3;
	int itemNumber;

	itemNumber = randInt(minItems,maxItems);
	if(itemNumber != 0){
		Pocket food(ITEM_FOOD,GetPocketName(ITEM_FOOD));
		for(int i=0; i<itemNumber; i++)
			food.items.push_back(GenerateItem(ITEM_FOOD,randInt(FOOD_NONE+1,FOOD_END-1)));
		self.push_back(food);
	}

	itemNumber = randInt(minItems,maxItems);
	if(itemNumber != 0){
		Pocket drink(ITEM_DRINK,GetPocketName(ITEM_DRINK));
		for(int i=0; i<itemNumber; i++)
			drink.items.push_back(GenerateItem(ITEM_DRINK,randInt(DRINK_NONE+1,DRINK_END-1)));
		self.push_back(drink);
	}

	itemNumber = randInt(minItems,maxItems);
	if(itemNumber != 0){
		Pocket daily(ITEM_DAILY,GetPocketName(ITEM_DAILY));
		for(int i=0; i<itemNumber; i++)
			daily.items.push_back(GenerateItem(ITEM_DAILY,randInt(DAILY_NONE+1,DAILY_END-1)));
		self.push_back(daily);
	}
}

void Inventory::GenerateSell(EJob aJob)
{
	int minItems = 2;
	int maxItems;
	int itemNumber;

	if(aJob == JOB_WEAPONSMITH)
	{
		maxItems = WEAPON_END-1;
		itemNumber = randInt(minItems,maxItems);
		Pocket pocket(ITEM_WEAPON,"Weapons");
		for(int i=0; i<itemNumber; i++)
			pocket.items.push_back(GenerateItem(ITEM_WEAPON,randInt(WEAPON_NONE+1,WEAPON_END-1)));
		sell.push_back(pocket);
	}
	if(aJob == JOB_ARMORSMITH)
	{
		maxItems = ARMOR_END-1;
		itemNumber = randInt(minItems,maxItems);
		Pocket pocket(ITEM_ARMOR,"Armor");
		for(int i=0; i<itemNumber; i++)
			pocket.items.push_back(GenerateItem(ITEM_ARMOR,randInt(ARMOR_NONE+1,ARMOR_END-1)));
		sell.push_back(pocket);
	}
	if(aJob == JOB_BARTENDER)
	{
		maxItems = DRINK_END-1;
		itemNumber = randInt(minItems,maxItems);
		Pocket pocket(ITEM_DRINK,"Drinks");
		for(int i=0; i<itemNumber; i++)
			pocket.items.push_back(GenerateItem(ITEM_DRINK,randInt(DRINK_NONE+1,DRINK_END-1)));
		sell.push_back(pocket);
	}
	if(aJob == JOB_HERBALIST)
	{
		EDaily itemSubTypes[] = {DAILY_WAX, DAILY_SAGE, DAILY_MINT, DAILY_ST_JOHNS_WORT, DAILY_TOADS_EYES, DAILY_LITTLE_EMPTY_FLASK};
		maxItems = 6;
		itemNumber = randInt(minItems,maxItems);
		Pocket pocket(ITEM_DAILY,"Alchemy");
		for(int i=0; i<itemNumber; i++)
			pocket.items.push_back(GenerateItem(ITEM_DAILY,itemSubTypes[rand()%maxItems]));
		sell.push_back(pocket);
	}
	if(aJob == JOB_PRIEST)
	{
		EDaily itemSubTypes[] = {DAILY_BANDAGE, DAILY_CPR_SCROLL, DAILY_PRAYER_BOOK, DAILY_PRAYER_SCARF,
			DAILY_SYMBOL_OF_THE_GODS, DAILY_MEDITATION_BLANKET};
		maxItems = 6;
		itemNumber = randInt(minItems,maxItems);
		Pocket pocket(ITEM_DAILY,"Sacrum");
		for(int i=0; i<itemNumber; i++)
			pocket.items.push_back(GenerateItem(ITEM_DAILY,itemSubTypes[rand()%maxItems]));
		sell.push_back(pocket);
	}
	if(aJob == JOB_SELLER)
	{
		maxItems = 10;
		itemNumber = randInt(minItems,maxItems);
		Pocket pocket(ITEM_FOOD,"Food");
		for(int i=0; i<itemNumber; i++)
			pocket.items.push_back(GenerateItem(ITEM_FOOD,randInt(FOOD_NONE+1,FOOD_END-1)));
		sell.push_back(pocket);

		maxItems = JEWELRY_END-1;
		itemNumber = randInt(minItems,maxItems);
		Pocket pocket3(ITEM_JEWELRY,"Jewelry");
		for(int i=0; i<itemNumber; i++)
			pocket3.items.push_back(GenerateItem(ITEM_JEWELRY,randInt(JEWELRY_NONE+1,JEWELRY_END-1)));
		sell.push_back(pocket3);

		maxItems = 10;
		itemNumber = randInt(minItems,maxItems);
		Pocket pocket2(ITEM_DAILY,"Misc");
		for(int i=0; i<itemNumber; i++)
			pocket2.items.push_back(GenerateItem(ITEM_DAILY,randInt(DAILY_NONE+1,DAILY_END-1)));
		sell.push_back(pocket2);
	}
	if(aJob == JOB_CHEF)
	{
		EFood itemSubTypes[] = {FOOD_LEG_OF_LAMB, FOOD_PIG_SNOUT, FOOD_OX_LIVER, FOOD_DONKEYS_EARS, FOOD_HORSES_HOOVES};
		maxItems = 5;
		itemNumber = randInt(minItems,maxItems);
		Pocket pocket(ITEM_FOOD,"Meals");
		for(int i=0; i<itemNumber; i++)
			pocket.items.push_back(GenerateItem(ITEM_FOOD,itemSubTypes[rand()%maxItems]));
		sell.push_back(pocket);
	}
}

void Inventory::AddItem(Item aItem)
{
	if(aItem.GetItemType() == ITEM_CLOTH)
	{
		clothes.push_back(aItem);
	}
	else
	{
		for(unsigned int i=0; i<self.size(); i++)
		{
			if(self[i].itemType == aItem.GetItemType())
			{
				self[i].items.push_back(aItem);
				return;
			}
		}

		Pocket p(aItem.GetItemType(),GetPocketName(aItem.GetItemType()));
		p.items.push_back(aItem);
		self.push_back(p);
	}
}

void Inventory::RemoveItem(Item* aItem)
{
	for(unsigned int i=0; i<self.size(); i++)
		{
			if(self[i].itemType == aItem->GetItemType())
			{
				for(unsigned int j=0; j<self[i].items.size(); j++)
				{
					if(self[i].items[j].GetItemSubType() == aItem->GetItemSubType())
					{
						self[i].items.erase(self[i].items.begin()+j);
						if(self[i].items.size() == 0)
							self.erase(self.begin()+i);
					}
				}
			}
		}
}

string Inventory::GetPocketName(EItem aType)
{
	switch(aType)
	{
	case ITEM_DAILY: return "Misc";
	case ITEM_CLOTH: return "Clothes";
	case ITEM_ARMOR: return "Armor";
	case ITEM_QUEST: return "Quest";
	case ITEM_WEAPON: return "Weapon";
	case ITEM_FOOD: return "Food";
	case ITEM_DRINK: return "Drinks";
	case ITEM_JEWELRY: return "Jewelry";
	default: return "Pocket";
	}
}